<?php
namespace app\admin\controller;
use clt\Leftnav;

/**
 * 新闻分类管理
 */
class NewsCate extends Common
{
	public function index()
	{
		if(request()->isPost()){
           	$nav  = new Leftnav();
			$data = db('news_cate') -> select();
        	$arr  = $nav->menu($data);
            return $result = ['code'=>0,'msg'=>'获取成功!','data'=>$arr,'rel'=>1];
        }
        return view();   
	}

	public function add()
	{
		if (request()->isPost()) {
            $data = input('post.');
            //添加
            if (db('news_cate')->insert($data)) {
                return ['code'=>1,'msg'=>'添加成功!','url'=>url('index')];
            } else {
                return ['code'=>0,'msg'=>'添加失败!'];
            }
        }

        //输出分类
    	$nav  = new Leftnav();
		$data = db('news_cate') -> select();
        $arr  = $nav->menu($data);
        $this-> assign('parent',$arr);
		return view();
	}

	public function edit()
	{
		if (request()->isPost()) {
            $data = input('post.');
            //添加
            if (db('news_cate')->update($data)) {
                return ['code'=>1,'msg'=>'修改成功!','url'=>url('index')];
            } else {
                return ['code'=>0,'msg'=>'修改失败!'];
            }
        }

        $id = input('param.id');
        $cate = db('news_cate') -> find($id);
        $this -> assign('cate', $cate);

        //输出分类
    	$nav  = new Leftnav();
		$data = db('news_cate') -> select();
        $arr  = $nav->menu($data);
        $this-> assign('parent',$arr);
		return view();
	}

	public function del()
	{
		if (request() -> isPost()) {
			$id = input('param.id');
			$result = db('news_cate') -> where(['pid'=>$id]) -> find();
			if (!empty($result)) {
				return ['code'=>0, 'msg'=>'存在子分类无法删除! ','url'=>url('index')];
			} else {
				db('news_cate') -> delete($id);
				return ['code'=>1, 'msg'=>'删除成功! ','url'=>url('index')];
			}
		}
	}
}